﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net;
using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using CommunitySystem.EmpbasicService;
using CommunitySystem.KaoHeService;
using AddEmp_DutyCompletedEventArgs = CommunitySystem.KaoHeService.AddEmp_DutyCompletedEventArgs;

namespace CommunitySystem
{
    public partial class KaoHeShangBaoWindow : ChildWindow
    {
        private KaoHeModel Model = new KaoHeModel();
        private KaoHeModel OldModel = new KaoHeModel();
        private KaoHe_AttatchmentModel jinshenatt = null;
        private KaoHeService.KaoHeServiceClient client = new KaoHeServiceClient();
        private EmpbasicService.EmpBasicServiceClient empclient = new EmpbasicService.EmpBasicServiceClient();
        public ObservableCollection<BianZhiModel> BianZhiModels = new ObservableCollection<BianZhiModel>();
        public KaoHeShangBaoWindow(KaoHeModel model)
        {
            InitializeComponent();
            OldModel = Model = model;
            empclient.GetBianZhiModelCompleted += new EventHandler<EmpbasicService.GetBianZhiModelCompletedEventArgs>(Client_GetBianZhiModelCompleted);
            empclient.GetBianZhiModelAsync(model.OrgID.ToString(), 0);
            client.AddEmp_DutyCompleted += new EventHandler<AddEmp_DutyCompletedEventArgs>(client_AddEmp_DutyCompleted);
            _uploadClient.UploadFileCompleted += new EventHandler<PublicOfficialSelectionService.UploadFileCompletedEventArgs>(_uploadClient_UploadFileCompleted);
            tt.OnSelectionChanged += new OnSelectionChangedEvent(tt_OnSelectionChanged);
            tt1.OnSelectionChanged += new OnSelectionChangedEvent(tt1_OnSelectionChanged);
            zhiweishuxing.OnSelectionChanged += new OnSelectionChangedEvent(zhiweishuxing_OnSelectionChanged);
            //if (model.Status == "称职")
            //{
            //    //chenzhisp.Visibility = System.Windows.Visibility.Visible;
            //    //zhuanrensp.Visibility = System.Windows.Visibility.Visible;
            //    zhuanrensp2.Visibility = System.Windows.Visibility.Visible;
            //    ss.Height = new GridLength(40);
            //}
            //else
            //{
            //    //chenzhisp.Visibility = System.Windows.Visibility.Collapsed;
            //    //zhuanrensp.Visibility = System.Windows.Visibility.Collapsed;
            //    zhuanrensp2.Visibility = System.Windows.Visibility.Collapsed;
            //    ss.Height = new GridLength(0);
            //}
            this.DataContext = Model;
            foreach (KaoHe_AttatchmentModel i in Model.Attachment)
            {
                StackPanel sp = new StackPanel() { Orientation = Orientation.Horizontal, };
                sp.Children.Add(new TextBlock() { Text = i.realname });
                Image img = new Image() { Cursor = Cursors.Hand, Height = 15, Width = 15, Tag = i.ID };
                img.Source = new System.Windows.Media.Imaging.BitmapImage(
                    new Uri("/CommunitySystem;component/Images/ViewDesign/delete.png",
                        UriKind.RelativeOrAbsolute));
                sp.Children.Add(img);
                wp.Children.Add(sp);
                img.MouseLeftButtonDown += (sender1, e1) =>
                                               {
                                                   int id = Convert.ToInt32(((Image)sender1).Tag);
                                                   wp.Children.Remove(sp);
                                                   Model.Attachment.Remove(Model.Attachment.Where(p => p.ID == id).FirstOrDefault());
                                               };
            }
            empclient.GetBianZhiDetailModelCompleted += new EventHandler<GetBianZhiDetailModelCompletedEventArgs>(Client2_GetBianZhiDetailModelCompleted);
            empclient.GetBianZhiDetailModelAsync(model.OrgID.ToString(), "");
        }
        void Client2_GetBianZhiDetailModelCompleted(object sender, GetBianZhiDetailModelCompletedEventArgs e)
        {
            BianZhiModels = e.Result;
            if (BianZhiModels != null && BianZhiModels.Count > 0)
            {
                string zhurenkeyong = "";
                int i1 =
                    Convert.ToInt32(
                        !string.IsNullOrEmpty(BianZhiModels.OrderBy(p => p.FuKeLingDao).FirstOrDefault().FuKeLingDao)
                            ? BianZhiModels.OrderBy(p => p.FuKeLingDao).FirstOrDefault().FuKeLingDao
                            : "0");
                int i2 =
                    Convert.ToInt32(
                        !string.IsNullOrEmpty(
                            BianZhiModels.OrderByDescending(p => p.zhurenshiyou).FirstOrDefault().zhurenshiyou)
                            ? BianZhiModels.OrderByDescending(p => p.zhurenshiyou).FirstOrDefault().zhurenshiyou
                            : "0");

                int i3 = 0;
                foreach (BianZhiModel b in BianZhiModels)
                {
                    if (!string.IsNullOrEmpty(b.ZhuRenJieYongQingKuang) && b.ZhuRenJieYongQingKuang != "无")
                    {
                        if (Math.Abs(Convert.ToInt32(b.ZhuRenJieYongQingKuang)) > i3)
                            i3 = Convert.ToInt32(b.ZhuRenJieYongQingKuang);
                    }
                }
                int i4 =
                    Convert.ToInt32(
                        BianZhiModels.OrderByDescending(p => p.ZhuRenChaoZhiQingKuang).FirstOrDefault().
                            ZhuRenChaoZhiQingKuang == "无"
                            ? "0"
                            : BianZhiModels.OrderByDescending(p => p.ZhuRenChaoZhiQingKuang).FirstOrDefault().
                                  ZhuRenChaoZhiQingKuang);
                zhurenkeyong = (i1 - i2 + i3 + i4).ToString();

                string fuzhurenkeyong = "";
                int f1 =
                    Convert.ToInt32(
                        !string.IsNullOrEmpty(
                            BianZhiModels.OrderBy(p => p.FuKeFeiLingDao).FirstOrDefault().FuKeFeiLingDao)
                            ? BianZhiModels.OrderBy(p => p.FuKeFeiLingDao).FirstOrDefault().FuKeFeiLingDao
                            : "0");
                int f2 =
                    Convert.ToInt32(
                        !string.IsNullOrEmpty(
                            BianZhiModels.OrderByDescending(p => p.fuzhurenshiyou).FirstOrDefault().fuzhurenshiyou)
                            ? BianZhiModels.OrderByDescending(p => p.fuzhurenshiyou).FirstOrDefault().fuzhurenshiyou
                            : "0");

                int f3 = 0;
                foreach (BianZhiModel b in BianZhiModels)
                {
                    if (!string.IsNullOrEmpty(b.FuZhuRenJieYongQingKuang) && b.FuZhuRenJieYongQingKuang != "无")
                    {
                        if (Math.Abs(Convert.ToInt32(b.FuZhuRenJieYongQingKuang)) > f3)
                            f3 = Convert.ToInt32(b.FuZhuRenJieYongQingKuang);
                    }
                }
                int f4 =
                    Convert.ToInt32(
                        BianZhiModels.OrderByDescending(p => p.FuZhuRenChaoZhiQingKuang).FirstOrDefault().
                            FuZhuRenChaoZhiQingKuang == "无"
                            ? "0"
                            : BianZhiModels.OrderByDescending(p => p.FuZhuRenChaoZhiQingKuang).FirstOrDefault().
                                  FuZhuRenChaoZhiQingKuang);
                fuzhurenkeyong = (f1 - f2 + f3 + f4).ToString();
                if (BianZhiModels.Where(p => p.ZhengKeLingDaoTC == true).Count() > 0)
                {
                    foreach (BianZhiModel i in BianZhiModels.Where(p => p.ZhengKeLingDaoTC == true))
                    {
                        int zhiwhu = !string.IsNullOrEmpty(i.ZhengKeLingDao) ? Convert.ToInt32(i.ZhengKeLingDao) : 0;
                        int renshu = BianZhiModels.Where(p => p.ZhengKeLingDaoTC == true).Sum(
                            p => !string.IsNullOrEmpty(p.zhengkeshiyou) ? Convert.ToInt32(p.zhengkeshiyou) : 0);
                        int jieyong = BianZhiModels.Where(p => p.ZhengKeLingDaoTC == true).Sum(
                            p =>
                            p.ZhengKeJieYongQingKuang != "无" ? Convert.ToInt32(p.ZhengKeJieYongQingKuang) : 0);
                        int chaozhi = BianZhiModels.Where(p => p.ZhengKeLingDaoTC == true).Sum(
                            p =>
                            p.ZhengKeChaoZhiQingKuang != "无" ? Convert.ToInt32(p.ZhengKeChaoZhiQingKuang) : 0);

                        i.ZhengKeShiJi =
                            (zhiwhu - renshu + jieyong + chaozhi)
                                .ToString();
                    }
                }

                if (BianZhiModels.Where(p => p.ZhengKeFeiLingDaoTC == true).Count() > 0)
                {
                    foreach (BianZhiModel i in BianZhiModels.Where(p => p.ZhengKeFeiLingDaoTC == true))
                    {
                        i.FuKeShiJi =
                            (Convert.ToInt32(
                                BianZhiModels.Where(p => p.ZhengKeFeiLingDaoTC == true).OrderBy(p => p.FuKeShiJi).
                                    FirstOrDefault().FuKeShiJi) +
                             BianZhiModels.Sum(
                                 p => p.FuKeJieYongQingKuang != "无" ? Convert.ToInt32(p.FuKeJieYongQingKuang) : 0) +
                             BianZhiModels.Sum(
                                 p => p.FuKeChaoZhiQingKuang != "无" ? Convert.ToInt32(p.FuKeChaoZhiQingKuang) : 0))
                                .ToString();

                        int zhiwhu = !string.IsNullOrEmpty(i.ZhengKeFeiLingDao)
                                         ? Convert.ToInt32(i.ZhengKeFeiLingDao)
                                         : 0;
                        int renshu = BianZhiModels.Where(p => p.ZhengKeFeiLingDaoTC == true).Sum(
                            p => !string.IsNullOrEmpty(p.fukeshiyou) ? Convert.ToInt32(p.fukeshiyou) : 0);
                        int jieyong = BianZhiModels.Where(p => p.ZhengKeFeiLingDaoTC == true).Sum(
                            p =>
                            p.FuKeJieYongQingKuang != "无" ? Convert.ToInt32(p.FuKeJieYongQingKuang) : 0);
                        int chaozhi = BianZhiModels.Where(p => p.ZhengKeFeiLingDaoTC == true).Sum(
                            p =>
                            p.FuKeChaoZhiQingKuang != "无" ? Convert.ToInt32(p.FuKeChaoZhiQingKuang) : 0);

                        i.ZhengKeShiJi =
                            (zhiwhu - renshu + jieyong + chaozhi)
                                .ToString();
                    }
                }
                foreach (BianZhiModel i in BianZhiModels)
                {
                    i.ZhuRenShiJi = zhurenkeyong;
                    i.FuZhuRenShiJi = fuzhurenkeyong;
                }
                if (BianZhiModels.Where(p => p.ZhengKeLingDaoTC == true).Count() > 0)
                {
                    foreach (BianZhiModel i in BianZhiModels.Where(p => p.ZhengKeLingDaoTC == true))
                    {
                        int zhiwhu = !string.IsNullOrEmpty(i.ZhengKeLingDao) ? Convert.ToInt32(i.ZhengKeLingDao) : 0;
                        int renshu = BianZhiModels.Where(p => p.ZhengKeLingDaoTC == true).Sum(
                            p => !string.IsNullOrEmpty(p.zhengkeshiyou) ? Convert.ToInt32(p.zhengkeshiyou) : 0);
                        int jieyong = BianZhiModels.Where(p => p.ZhengKeLingDaoTC == true).Sum(
                            p =>
                            p.ZhengKeJieYongQingKuang != "无" ? Convert.ToInt32(p.ZhengKeJieYongQingKuang) : 0);
                        int chaozhi = BianZhiModels.Where(p => p.ZhengKeLingDaoTC == true).Sum(
                            p =>
                            p.ZhengKeChaoZhiQingKuang != "无" ? Convert.ToInt32(p.ZhengKeChaoZhiQingKuang) : 0);

                        i.ZhengKeShiJi =
                            (zhiwhu - renshu + jieyong + chaozhi)
                                .ToString();
                    }
                }

                if (BianZhiModels.Where(p => p.ZhengKeFeiLingDaoTC == true).Count() > 0)
                {
                    foreach (BianZhiModel i in BianZhiModels.Where(p => p.ZhengKeFeiLingDaoTC == true))
                    {
                        i.FuKeShiJi =
                            (Convert.ToInt32(
                                BianZhiModels.Where(p => p.ZhengKeFeiLingDaoTC == true).OrderBy(p => p.FuKeShiJi).
                                    FirstOrDefault().FuKeShiJi) +
                             BianZhiModels.Sum(
                                 p => p.FuKeJieYongQingKuang != "无" ? Convert.ToInt32(p.FuKeJieYongQingKuang) : 0) +
                             BianZhiModels.Sum(
                                 p => p.FuKeChaoZhiQingKuang != "无" ? Convert.ToInt32(p.FuKeChaoZhiQingKuang) : 0))
                                .ToString();

                        int zhiwhu = !string.IsNullOrEmpty(i.ZhengKeFeiLingDao) ? Convert.ToInt32(i.ZhengKeFeiLingDao) : 0;
                        int renshu = BianZhiModels.Where(p => p.ZhengKeFeiLingDaoTC == true).Sum(
                            p => !string.IsNullOrEmpty(p.fukeshiyou) ? Convert.ToInt32(p.fukeshiyou) : 0);
                        int jieyong = BianZhiModels.Where(p => p.ZhengKeFeiLingDaoTC == true).Sum(
                            p =>
                            p.FuKeJieYongQingKuang != "无" ? Convert.ToInt32(p.FuKeJieYongQingKuang) : 0);
                        int chaozhi = BianZhiModels.Where(p => p.ZhengKeFeiLingDaoTC == true).Sum(
                            p =>
                            p.FuKeChaoZhiQingKuang != "无" ? Convert.ToInt32(p.FuKeChaoZhiQingKuang) : 0);

                        i.ZhengKeShiJi =
                            (zhiwhu - renshu + jieyong + chaozhi)
                                .ToString();
                    }
                }
                
                
            }
        }
        void Client_GetBianZhiModelCompleted(object sender, EmpbasicService.GetBianZhiModelCompletedEventArgs e)
        {
            biaoti.ItemsSource = null;
            biaoti.ItemsSource = e.Result;
            if (biaoti.ItemsSource != null)
            {
                for (int i = 0; i < ((ObservableCollection<BianZhiModel>)biaoti.ItemsSource).Count; i++)
                {
                    if (((ObservableCollection<BianZhiModel>)biaoti.ItemsSource)[i].JiGouMingCheng == OldModel.KeShi)
                        biaoti.SelectedIndex = i;
                }
            }
        }
        void gangwei_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (((ComboBox)sender).SelectedItem != null)
            {
                OldModel.KeShi = ((BianZhiModel)((((ComboBox)sender).SelectedItem))).JiGouMingCheng;
            }

        }
        void zhiweishuxing_OnSelectionChanged(object sender, object NewValue, object OldValue)
        {
            if (NewValue != null)
            {
                if (NewValue.ToString() == "领导职务")
                {
                    zhuanrensp2.Visibility = System.Windows.Visibility.Visible;
                    ss.Height = new GridLength(40);
                }
                else
                {
                    //chenzhisp.Visibility = System.Windows.Visibility.Collapsed;
                    //zhuanrensp.Visibility = System.Windows.Visibility.Collapsed;
                    zhuanrensp2.Visibility = System.Windows.Visibility.Collapsed;
                    Model.Begindate = Model.Enddate = null;
                    ss.Height = new GridLength(0);
                }
            }
            
        }

        private bool can = false;
        void tt1_OnSelectionChanged(object sender, object NewValue, object OldValue)
        {
            //if(string.IsNullOrEmpty(OldModel.RenZhiStatus))
            if (NewValue != null)
            {
                if (NewValue.ToString() == "转任" || NewValue.ToString() == "连任")
                {
                    zhuanrensp.Visibility = System.Windows.Visibility.Visible;
                    zhuanrensp2.Visibility = System.Windows.Visibility.Visible;
                    if (NewValue.ToString() == "连任")
                    {
                        Model.ZhiWei = Model.OldZhiWei;
                        Model.NewZhiWuShuXing = Model.ZhiWuShuXing;
                        Model.KeShi = Model.OldKeShi;
                        zhiwei.DictCode = "zwdm";
                        zhiweishuxing.DictCode = "zwsx";

                        //zhiweishuxing.DataContent = this;

                    }
                    if (NewValue.ToString() == "转任")
                    {
                        if (can == false)
                        {
                            if (Model.OldKeShi != Model.KeShi && string.IsNullOrEmpty(Model.KeShi))
                                Model.KeShi = null;
                            if (Model.OldZhiWei != Model.ZhiWei && string.IsNullOrEmpty(Model.ZhiWei))
                                Model.ZhiWei = null;
                            if (Model.ZhiWuShuXing != Model.NewZhiWuShuXing && string.IsNullOrEmpty(Model.NewZhiWuShuXing))
                                Model.NewZhiWuShuXing = null;
                            zhiwei.DictCode = "zwdm";
                            zhiweishuxing.DictCode = "zwsx";
                        }
                        else
                        {
                            Model.KeShi = null;
                            Model.ZhiWei = null;
                            Model.Begindate = null;
                            Model.Enddate = null;
                            Model.NewZhiWuShuXing = null;
                            zhiwei.DictCode = "zwdm";
                            zhiweishuxing.DictCode = "zwsx";
                        }

                    }
                }
                else
                {
                    zhuanrensp.Visibility = System.Windows.Visibility.Collapsed;
                    zhuanrensp2.Visibility = System.Windows.Visibility.Collapsed;
                    Model.KeShi = null;
                    Model.ZhiWei = null;
                    Model.NianShu = null;
                    zhiwei.DictCode = "zwdm";
                    zhiweishuxing.DictCode = "zwsx";
                }
                can = true;
            }
            
            //this.DataContext = Model;
        }

        void tt_OnSelectionChanged(object sender, object NewValue, object OldValue)
        {
            //if (string.IsNullOrEmpty(OldModel.Status))
            if (NewValue != null)
            {
                if (NewValue.ToString() == "称职" || NewValue.ToString() == "优秀")
                {
                    //chenzhisp.Visibility = System.Windows.Visibility.Visible;
                    //zhuanrensp.Visibility = System.Windows.Visibility.Visible;
                    if (Model.RenZhiStatus != "暂不任岗位")
                        zhuanrensp2.Visibility = System.Windows.Visibility.Visible;
                    else
                        zhuanrensp2.Visibility = System.Windows.Visibility.Collapsed;
                    ss.Height = new GridLength(40);
                    tt1.Visibility = System.Windows.Visibility.Visible;
                    tt2.Visibility = System.Windows.Visibility.Collapsed;
                    zhiwei.Visibility = System.Windows.Visibility.Visible;
                    zhiweishuxing.Visibility = System.Windows.Visibility.Visible;
                    zhiwei1.Visibility = System.Windows.Visibility.Collapsed;
                    zhiweishuxing1.Visibility = System.Windows.Visibility.Collapsed;
                }
                else
                {
                    //chenzhisp.Visibility = System.Windows.Visibility.Collapsed;
                    //zhuanrensp.Visibility = System.Windows.Visibility.Collapsed;
                    ss.Height = new GridLength(0);
                    zhuanrensp2.Visibility = System.Windows.Visibility.Collapsed;
                    tt1.Visibility = System.Windows.Visibility.Collapsed;
                    tt2.Visibility = System.Windows.Visibility.Visible;
                    zhiwei1.Visibility = System.Windows.Visibility.Visible;
                    zhiweishuxing1.Visibility = System.Windows.Visibility.Visible;
                    zhiwei.Visibility = System.Windows.Visibility.Collapsed;
                    zhiweishuxing.Visibility = System.Windows.Visibility.Collapsed;
                    Model.RenZhiStatus = null;
                    Model.KeShi = null;
                    Model.ZhiWei = null;
                    Model.NianShu = null;
                }
            }
            
        }
        private readonly string fileDictionary = "file";
        private Dictionary<string, UploadFileInfo> files = new Dictionary<string, UploadFileInfo>();
        private void hlb_Click(object sender, RoutedEventArgs e)
        {
            //if (Model.Attachment != null)
            //{
            //    if (Model.Attachment.Count > 0)
            //        Model.Attachment.Clear();
            //}
            var openFile = new OpenFileDialog() { Multiselect = false };
            if (openFile.ShowDialog() == true)
            {
                string GuidName = Guid.NewGuid().ToString("N");
                // files.Add(GuidName, openFile.File.OpenRead());
                UploadFileInfo uploadFile = new UploadFileInfo() { DisplayName = GuidName, Name = GuidName };
                var stream = openFile.File.OpenRead();
                uploadFile.Size = stream.Length / 16000;
                uploadFile.Context = new List<byte[]>();
                int b = 0;
                while (stream.Position > -1 && stream.Position < stream.Length)
                {
                    if (stream.Length - stream.Position >= 16000)
                    {
                        b = 16000;
                    }
                    else
                    {
                        b = (int)(stream.Length - stream.Position);
                    }

                    byte[] filebyte = new byte[b];
                    stream.Read(filebyte, 0, b);
                    uploadFile.Context.Add(filebyte);
                }
                stream.Close();
                files.Add(GuidName, uploadFile);
                StackPanel sp = new StackPanel() { Orientation = Orientation.Horizontal, VerticalAlignment = System.Windows.VerticalAlignment.Center };
                sp.Children.Add(new TextBlock() { Text = openFile.File.Name, });
                Image img = new Image() { Cursor = Cursors.Hand, Height = 15, Width = 15, Margin = new Thickness(5, 0, 0, 0) };
                img.Source = new System.Windows.Media.Imaging.BitmapImage(
                    new Uri("/CommunitySystem;component/Images/ViewDesign/delete.png",
                        UriKind.RelativeOrAbsolute));
                sp.Children.Add(img);

                if (Model.Attachment == null)
                    Model.Attachment = new System.Collections.ObjectModel.ObservableCollection<KaoHe_AttatchmentModel>();
                var att = new KaoHe_AttatchmentModel() { realname = openFile.File.Name, filename = GuidName, Key = fileDictionary };
                Model.Attachment.Add(att);
                jinshenatt = Model.Attachment.First();
                img.MouseLeftButtonDown += (sender1, e1) =>
                {
                    wp.Children.Remove(sp);
                    files.Remove(GuidName);
                    Model.Attachment.Remove(att);
                    jinshenatt = null;
                };
                wp.Children.Add(sp);
            }
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }

        private bool Validate()
        {
            if (Model.TotalCount <= 0 || Model.TotalCount == null)
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage("应参加评测人数填写错误!");
                return false;
            }
            if (Model.Realcount <= 0 || Model.Realcount == null)
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage("实到人数填写错误!");
                return false;
            }
            if (Model.Realcount > Model.TotalCount)
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage("实到人数不能大于应参加人数!");
                return false;
            }

            if (Model.Realcount > Model.TotalCount)
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage("实到人数不能大于应参加人数!");
                return false;
            }
            if (Convert.ToInt32(Model.YouXiu) < 0)
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage("优秀的票数不能小于0");
                return false;
            }

            if (Convert.ToInt32(Model.ChenZhi) < 0)
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage("称职的票数不能小于0");
                return false;
            }

            if (Convert.ToInt32(Model.BuChenZhi) < 0)
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage("不称职的票数不能小于0");
                return false;
            }
            if (Convert.ToInt32(Model.QiQuan) < 0)
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage("弃权的票数不能小于0");
                return false;
            }
            if (Convert.ToInt32(Model.UnAgreeRate) < 0)
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage("不称职率的不能小于0");
                return false;
            }
            if (Model.RenZhiStatus == "转任")
            {
                if (Convert.ToInt32(Model.YouXiu) + Convert.ToInt32(Model.ChenZhi) + Convert.ToInt32(Model.BuChenZhi) + Convert.ToInt32(Model.QiQuan) > Convert.ToInt32(Model.Realcount))
                {
                    SilverlightMessageBoxLibrary.Message.ErrorMessage("总票数不能大于实到人数");
                    return false;
                }

                if (Model.ZhiWuCengCi == "乡科级正职（主任科员）" && Model.ZhiWuShuXing == "领导职务")
                {
                    if (BianZhiModels.Where(p => p.OrgID == Model.OrgID && p.JiGouMingCheng == Model.KeShi && Convert.ToInt32(!string.IsNullOrEmpty(p.ZhengKeShiJi) ? p.ZhengKeShiJi : "0") > 0).FirstOrDefault() == null)
                    {
                        SilverlightMessageBoxLibrary.Message.ErrorMessage("岗位职数不足，请与主管部门联系！");
                        return false;
                    }
                }
                if (Model.ZhiWuCengCi == "乡科级副职（副主任科员）" && Model.ZhiWuShuXing == "领导职务")
                {
                    if (BianZhiModels.Where(p => p.OrgID == Model.OrgID && p.JiGouMingCheng == Model.KeShi && Convert.ToInt32(!string.IsNullOrEmpty(p.FuKeShiJi) ? p.FuKeShiJi : "0") > 0).FirstOrDefault() == null)
                    {
                        SilverlightMessageBoxLibrary.Message.ErrorMessage("岗位职数不足，请与主管部门联系！");
                        return false;
                    }
                }
                if (Model.ZhiWuCengCi == "乡科级正职（主任科员）" && Model.ZhiWuShuXing == "非领导职务")
                {
                    if (BianZhiModels.Where(p => p.OrgID == Model.OrgID && p.JiGouMingCheng == Model.KeShi && Convert.ToInt32(!string.IsNullOrEmpty(p.ZhuRenShiJi) ? p.ZhuRenShiJi : "0") > 0).FirstOrDefault() == null)
                    {
                        SilverlightMessageBoxLibrary.Message.ErrorMessage("岗位职数不足，请与主管部门联系！");
                        return false;
                    }
                }
                if (Model.ZhiWuCengCi == "乡科级副职（副主任科员）" && Model.ZhiWuShuXing == "非领导职务")
                {
                    if (BianZhiModels.Where(p => p.OrgID == Model.OrgID && p.JiGouMingCheng == Model.KeShi && Convert.ToInt32(!string.IsNullOrEmpty(p.FuZhuRenShiJi) ? p.FuZhuRenShiJi : "0") > 0).FirstOrDefault() == null)
                    {
                        SilverlightMessageBoxLibrary.Message.ErrorMessage("岗位职数不足，请与主管部门联系！");
                        return false;
                    }
                }
            }
            
            return true;
        }

        private PublicOfficialSelectionService.PublicOfficialSelectionServiceClient _uploadClient =
            new PublicOfficialSelectionService.PublicOfficialSelectionServiceClient();
        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            if(Validate())
            {
                if (files.Count > 0)
                {
                    #region 上传文件
                    foreach (var key in files.Keys)
                    {
                        var file = files[key];
                        _uploadClient.UploadFileAsync(fileDictionary, file.DisplayName, file.Context[0], true, file);
                        file.Context.RemoveAt(0);

                    }
                    #endregion
                }
                else
                {
                    client.AddEmp_DutyAsync(Model);
                }
            }
            
        }
        void _uploadClient_UploadFileCompleted(object sender, PublicOfficialSelectionService.UploadFileCompletedEventArgs e)
        {
            var file = e.UserState as UploadFileInfo;
            if (file.Context.Count > 0)
            {
                _uploadClient.UploadFileAsync(fileDictionary, file.DisplayName, file.Context[0], false, file);
                file.Context.RemoveAt(0);
            }
            client.AddEmp_DutyAsync(Model);
        }

        private int count = 1;
        void client_AddEmp_DutyCompleted(object sender, AddEmp_DutyCompletedEventArgs e)
        {
            count = count - 1;
            if (count == 0)
            {
                SilverlightMessageBoxLibrary.Message.InfoMessage("上报成功！");
                this.DialogResult = true;
            }
        }

        private void TextBox_LostFocus2(object sender, RoutedEventArgs e)
        {
            int count = (int)Model.YouXiu + (int)Model.ChenZhi + (int)Model.BuChenZhi + (int)Model.QiQuan;
            if (count > Convert.ToInt32(Model.Realcount))
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage("总票数不能大于实到人数");
                return;
            }
            if (count > 0)
            {
                var rate = (int)Model.BuChenZhi * 100.0 / count;
                Model.UnAgreeRate = (decimal)Math.Round(rate, 2);
            }
        }

        private string pattern = @"^[0-9]+([.]{1}[0-9]+){0,1}$";
        private void TextBox_LostFocus1(object sender, RoutedEventArgs e)
        {
            Match m = Regex.Match(((TextBox)sender).Text, pattern); // 匹配正则表达式
            if (!m.Success) // 输入的不是数字
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage("只能输入数字！");
                ((TextBox)sender).Text = "";
                return;
            }
        }
    }
}
